# analysis/trend_analysis.py
from pyspark.sql import DataFrame


class TrendAnalyzer:
    @staticmethod
    def analyze_daily_trend(df: DataFrame) -> DataFrame:
        return (df
                .groupBy("date")
                .agg({"visits": "sum",
                      "bounce_rate": "avg",
                      "avg_session_duration": "avg",
                      "conversion_rate": "avg"})
                .withColumnRenamed("sum(visits)", "total_visits")
                .withColumnRenamed("avg(bounce_rate)", "avg_bounce_rate")
                .withColumnRenamed("avg(avg_session_duration)", "avg_session_duration")
                .withColumnRenamed("avg(conversion_rate)", "avg_conversion_rate")
                .orderBy("date"))

    @staticmethod
    def analyze_weekly_trend(df: DataFrame) -> DataFrame:
        return (df
                .groupBy("week")
                .agg({"visits": "sum",
                      "conversion_rate": "avg"})
                .withColumnRenamed("sum(visits)", "weekly_visits")
                .withColumnRenamed("avg(conversion_rate)", "avg_conversion_rate")
                .orderBy("week"))